import time
from Common.basepage import BasePage
from PageLocators.ALL_locator import loc
from Common.handle_log import do_log
from Common.connect_to_device import *


# 首页操作
class MainPage(BasePage):

    def click_mine(self):
            self.click_element(loc.main_enter_mine_Loc, "主页_点击个人中心")
            time.sleep(0.5)


    def click_history(self):
        if loc.main_enter_history_Loc:
            history_loc1=f'(resourceId="{package_name}:id/fl_history")'
            if self.judge_ele_exists(loc.main_enter_history_Loc):
                self.click_element(loc.main_enter_history_Loc,"主页_点击历史记录")
            else:
                self.click_element(history_loc1,"主页_点击历史记录")
            time.sleep(0.5)
    def click_history_back(self):
        if self.judge_ele_exists(loc.history_back_loc):
            self.click_element(loc.history_back_loc, "步骤NOTPLAY_004_搜索页面返回频道")
            time.sleep(1)

    def click_search(self):

        self.click_element(loc.main_enter_search_Loc, "主页_点击进入搜索页面")
        time.sleep(1)
    def click_search_back(self):
        if self.judge_ele_exists(loc.search_back_loc):
            self.click_element(loc.search_back_loc, "步骤NOTPLAY_004_搜索页面返回频道")
            time.sleep(1)


    def search_byText(self,text):

        img_desc = "搜索页输入框输入'"+text+"'"
        self.input_text(loc.search_edit_search_loc, text, img_desc)
        time.sleep(1)
        self.click_element(loc.search_tv_search_loc, "点击搜索页'搜索'按钮")
        time.sleep(2)
        i=0
        while not self.judge_ele_exists(loc.search_playNo1) and i<5:
            self.click_element(loc.search_tv_search_loc, "点击搜索页'搜索'按钮")
            time.sleep(2)
            i+=1




    def click_playNo1(self):
            time.sleep(1)

            self.click_element(loc.search_playNo1, "精确搜索结果页-点击播放")
            for i in range(2):
                if self.judge_ele_exists(loc.search_playNo1):
                    self.click_element(loc.search_playNo1, "精确搜索结果页-点击播放")
                    time.sleep(1)



    def enter_to_main_page(self):

        main_page_exit=self.judge_ele_exists(loc.main_tab_loc)

        if  main_page_exit:
            do_log.info("当前页面就在首页,无需执行命令")
            return True
        i=1
        while not (main_page_exit) and i<6:
            d_one_device.shell("input keyevent 4")
            time.sleep(2)
            main_page_exit = self.judge_ele_exists(loc.main_tab_loc)
            time.sleep(2)
            i+=1
        if main_page_exit:
            do_log.info("当前通过遥控器方式回到首页成功")
            return True
        else:
            do_log.error("当前通过遥控器方式回到首页失败")
            return False





    def get_page_name(self):
        """首页，播放页，搜索页，历史记录页,我的收藏页,个人设置页,个人中心页"""
        page_list = {"MainActivity": "首页",
                     "PlayerActivity": "播放页",
                     "SearchActivity": "搜索页",
                     "HistoryActivity": "历史记录页",
                     "CollectActivity": "我的收藏页",
                     "SettingActivity": "个人设置页",
                     "UserCenterActivity": "个人中心页",
                     "RankActivity":"风云榜",
                     "FilterActivity":"筛选页",
                     "EditNavActivity":"频道编辑页",
                     "LoginActivity":"个人登录页",
                     "VipOpenActivity":"会员购买页",
                     "HtmlActivity":"第三方页面"

                     }
        res = d_one.app_current()["activity"]
        if "arcvideo" not in res:
            do_log.error(f"当前页面不在爱奇艺应用,请注意")
            return "离开爱奇艺页面"
        for key in page_list.keys():
            if key in res:
                do_log.info(f"当前页面为{page_list[key]}")
                return page_list[key]

        do_log.info(f"当前页面为爱奇艺其他页面")
        return ""
    def wake_seekbar(self):
        for i in range(3):
            if not self.judge_ele_exists(loc.play_back_loc):
                if full_screen==-1:
                    self.click_point_by_score(0.25, 0.5, "点击屏幕唤起播控键等")
                elif full_screen==1:
                    self.click_point_by_score(0.75, 0.5, "点击屏幕唤起播控键等")
                else:
                    self.click_point_by_score(0.5, 0.5, "点击屏幕唤起播控键等")
                time.sleep(1)
            return None

    # 切换音频模式
    def set_audio_mode(self):
        self.wake_seekbar()
        self.click_element(loc.audio_mode_loc, "播放页_点击音频模式")
        time.sleep(1)
        i = 30
        while i > 0 and self.judge_ele_exists(loc.audio_loading_loc):
            i -= 1
            time.sleep(1.5)
    def refresh(self):
        """检测网络异常问题"""
        texts = ["刷新一下","点击重试","刷新页面"]
        for text in texts:
            if self.judge_ele_exists(self.get_text_loc(text)):
                self.click_text_loc(text)
                time.sleep(1)
        time.sleep(1)


if __name__ == '__main__':
    d = MainPage(device="d_one")
    d.enter_to_main_page()

